package cn.iocoder.yudao.adminserver.franky.single;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @Description:
 * @Author: franky
 * @CreateDate: 2021/4/15 9:13 下午
 * @Version: 1.0
 */
public class IdGenerator {
    private AtomicLong atomic = new AtomicLong(0);

    private static IdGenerator instance;

    private IdGenerator(){}

    public static synchronized IdGenerator getInstance() {
        if (instance == null) {
            return new IdGenerator();
        }
        return instance;
    }

    private static class SingletonHolder {
        private static final IdGenerator i  = new IdGenerator();
    }

    public static IdGenerator getInstance2() {
        if (instance == null) {
            synchronized (IdGenerator.class) {
                if(instance == null) {
                    return new IdGenerator();
                }
            }
        }
        return instance;
    }

    public long getId() {
        return atomic.incrementAndGet();
    }


}
